【题解】 历史研究 回滚莫队 luoguAT1219 | Qiuly's blog!

【题解】 历史研究 回滚莫队 luoguAT1219

回滚莫队板子题,在此不再赘述。

关于回滚莫队的文章戳这 $QwQ$ :[算法]浅谈4种莫队及例题

可以算作一个回滚莫队的板子来参考。

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;

const int N=1e5+2;
const int inf=1e9+9;

int n,m,block,a[N];
template <typename _Tp> inline void IN(_Tp&x){
char ch;bool flag=0;x=0;
while(ch=getchar(),!isdigit(ch))if(flag)x=-x;
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
if(flag)x=-x;
}

namespace MO{
ll ans,Ans[N];
int v[N],b[N],blo[N],cnt[N],hep[N];
struct Query{int l,r,id;}q[N];
bool cmp(Query x,Query y){
return blo[x.l]==blo[y.l]?x.r<y.r:blo[x.l]<blo[y.l];
}
inline void roll(int pos){--cnt[b[pos]];}
inline void add(int pos){
++cnt[b[pos]];
ans=max(ans,1ll*a[pos]*cnt[b[pos]]);
}
inline ll query(int l,int r){
ll res=0;
for(int i=l;i<=r;++i)hep[b[i]]=0;
for(int i=l;i<=r;++i){
++hep[b[i]];
res=max(res,1ll*a[i]*hep[b[i]]);
}return res;
}
inline int solve(int num,int bloid){
int L=min(block*bloid,n);
int i=num,l=L+1,r=l-1;
for(int k=1;k<=n;++k)cnt[k]=0;
ans=0;
for(;blo[q[i].l]==bloid;++i){
if(blo[q[i].l]==blo[q[i].r]){
Ans[q[i].id]=query(q[i].l,q[i].r);
continue;
}
while(r<q[i].r)add(++r);
ll tmp=ans;
while(l>q[i].l)add(--l);
Ans[q[i].id]=ans;
while(l<L+1)roll(l++);
ans=tmp;
}return i;
}
inline void Main(){
sort(v+1,v+1+n);
int tot=unique(v+1,v+1+n)-(v+1);
for(int i=1;i<=n;++i)
b[i]=lower_bound(v+1,v+1+tot,a[i])-v;
sort(q+1,q+1+m,cmp);
int num=1;
for(int i=1;i<=blo[n];++i)num=solve(num,i);
for(int i=1;i<=m;++i)printf("%lld\n",Ans[i]);
return;
}
}
using namespace MO;
int main(){
IN(n),IN(m);block=sqrt(n);
for(int i=1;i<=n;++i)IN(a[i]),v[i]=a[i];
for(int i=1;i<=n;++i)blo[i]=(i-1)/block+1;
for(int i=1;i<=m;++i){
IN(q[i].l),IN(q[i].r);
q[i].id=i;
}return Main(),0;
}

本文标题:【题解】 历史研究 回滚莫队 luoguAT1219

文章作者:Qiuly

发布时间:2019年03月20日 - 00:00

最后更新:2019年03月29日 - 13:52

原始链接:http://qiulyblog.github.io/2019/03/20/[题解]luoguAT1219/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。